"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IpUtil = void 0;
const Logger_1 = require("./Logger");
const fs = require("fs");
const readline = require("readline");
class IpUtil {
    /**
     * 根据文件地址加载ip地址库
     * @param filePath
     */
    static loadFile(filePath) {
        Logger_1.Logger.log("loadFile=", filePath);
        let fRead = fs.createReadStream(filePath);
        var objReadLine = readline.createInterface({
            input: fRead
        });
        objReadLine.on('line', (line) => {
            var obj = JSON.parse(line);
            this.ipCountryMap[obj.ip] = obj.country;
            // Logger.log("ipCountryMap==", this.ipCountryMap)
        });
    }
    static isInChina(ip = "113.111.184.29") {
        let countryCode = this.getCountryCode(ip);
        Logger_1.Logger.log("isInChina==", countryCode);
        if (countryCode == this.codeLib.CHN || countryCode == this.codeLib.HKG) {
            return true;
        }
        return false;
    }
    /**
     * ipv4地址字符串转数字
     * @param ip
     * @returns {number}
     */
    static ipToNumber(ip) {
        let result = ip.split(".");
        if (!result)
            return -1;
        return (parseInt(result[0]) << 24
            | parseInt(result[1]) << 16
            | parseInt(result[2]) << 8
            | parseInt(result[3])) >>> 0;
    }
    /**
     * 根据ip获取国家代码
     * @param ip
     * @returns {number}
     */
    static getCountryCode(ip) {
        let ipNum = this.ipToNumber(ip);
        Logger_1.Logger.log("ipNum===", ipNum);
        var countryCode = this.ipCountryMap[this.cClassIpNum(ipNum)];
        Logger_1.Logger.log("countryCode===", countryCode, this.ipCountryMap);
        if (countryCode === undefined) {
            countryCode = 0;
        }
        return countryCode;
    }
    /**
     * IPv4地址分配是按C段来分，所以干掉后8位，保留前24位即可
     * @param ipNum
     * @returns {number}
     */
    static cClassIpNum(ipNum) {
        return ipNum >>> 8;
    }
}
exports.IpUtil = IpUtil;
/**
 * 国家代码，参照维基百科2018年7月3日 (星期二) 06:08版国际奥委会国家编码列表
 * @type {{AFG: number, ALB: number, ALG: number, AND: number, ANG: number, ANT: number, ARG: number, ARM: number, ARU: number, ASA: number, AUS: number, AUT: number, AZE: number, BAH: number, BAN: number, BAR: number, BDI: number, BEL: number, BEN: number, BER: number, BHU: number, BIH: number, BIZ: number, BLR: number, BOL: number, BOT: number, BRA: number, BRN: number, BRU: number, BUL: number, BUR: number, CAF: number, CAM: number, CAN: number, CAY: number, CGO: number, CHA: number, CHI: number, CHN: number, CIV: number, CMR: number, COD: number, COK: number, COL: number, COM: number, CPV: number, CRC: number, CRO: number, CUB: number, CYP: number, CZE: number, DEN: number, DJI: number, DMA: number, DOM: number, ECU: number, EGY: number, ERI: number, ESA: number, ESP: number, EST: number, ETH: number, FIJ: number, FIN: number, FRA: number, FSM: number, GAB: number, GAM: number, GBR: number, GBS: number, GEO: number, GEQ: number, GER: number, GHA: number, GRE: number, GRN: number, GUA: number, GUI: number, GUM: number, GUY: number, HAI: number, HKG: number, HON: number, HUN: number, INA: number, IND: number, IRI: number, IRL: number, IRQ: number, ISL: number, ISR: number, ISV: number, ITA: number, IVB: number, JAM: number, JOR: number, JPN: number, KAZ: number, KEN: number, KGZ: number, KIR: number, KOR: number, KOS: number, KSA: number, KUW: number, LAO: number, LAT: number, LBA: number, LBN: number, LBR: number, LCA: number, LES: number, LIE: number, LTU: number, LUX: number, MAD: number, MAR: number, MAS: number, MAW: number, MDA: number, MDV: number, MEX: number, MGL: number, MHL: number, MKD: number, MLI: number, MLT: number, MNE: number, MON: number, MOZ: number, MRI: number, MTN: number, MYA: number, NAM: number, NCA: number, NED: number, NEP: number, NGR: number, NIG: number, NOR: number, NRU: number, NZL: number, OMA: number, PAK: number, PAN: number, PAR: number, PER: number, PHI: number, PLE: number, PLW: number, PNG: number, POL: number, POR: number, PRK: number, PUR: number, QAT: number, ROU: number, RSA: number, RUS: number, RWA: number, SAM: number, SEN: number, SEY: number, SGP: number, SKN: number, SLE: number, SLO: number, SMR: number, SOL: number, SOM: number, SRB: number, SRI: number, SSD: number, STP: number, SUD: number, SUI: number, SUR: number, SVK: number, SWE: number, SWZ: number, SYR: number, TAN: number, TGA: number, THA: number, TJK: number, TKM: number, TLS: number, TOG: number, TPE: number, TTO: number, TUN: number, TUR: number, TUV: number, UAE: number, UGA: number, UKR: number, URU: number, USA: number, UZB: number, VAN: number, VEN: number, VIE: number, VIN: number, YEM: number, ZAM: number, ZIM: number}}
 */
IpUtil.codeLib = {
    "AFG": 1,
    "ALB": 2,
    "ALG": 3,
    "AND": 4,
    "ANG": 5,
    "ANT": 6,
    "ARG": 7,
    "ARM": 8,
    "ARU": 9,
    "ASA": 10,
    "AUS": 11,
    "AUT": 12,
    "AZE": 13,
    "BAH": 14,
    "BAN": 15,
    "BAR": 16,
    "BDI": 17,
    "BEL": 18,
    "BEN": 19,
    "BER": 20,
    "BHU": 21,
    "BIH": 22,
    "BIZ": 23,
    "BLR": 24,
    "BOL": 25,
    "BOT": 26,
    "BRA": 27,
    "BRN": 28,
    "BRU": 29,
    "BUL": 30,
    "BUR": 31,
    "CAF": 32,
    "CAM": 33,
    "CAN": 34,
    "CAY": 35,
    "CGO": 36,
    "CHA": 37,
    "CHI": 38,
    "CHN": 39,
    "CIV": 40,
    "CMR": 41,
    "COD": 42,
    "COK": 43,
    "COL": 44,
    "COM": 45,
    "CPV": 46,
    "CRC": 47,
    "CRO": 48,
    "CUB": 49,
    "CYP": 50,
    "CZE": 51,
    "DEN": 52,
    "DJI": 53,
    "DMA": 54,
    "DOM": 55,
    "ECU": 56,
    "EGY": 57,
    "ERI": 58,
    "ESA": 59,
    "ESP": 60,
    "EST": 61,
    "ETH": 62,
    "FIJ": 63,
    "FIN": 64,
    "FRA": 65,
    "FSM": 66,
    "GAB": 67,
    "GAM": 68,
    "GBR": 69,
    "GBS": 70,
    "GEO": 71,
    "GEQ": 72,
    "GER": 73,
    "GHA": 74,
    "GRE": 75,
    "GRN": 76,
    "GUA": 77,
    "GUI": 78,
    "GUM": 79,
    "GUY": 80,
    "HAI": 81,
    "HKG": 82,
    "HON": 83,
    "HUN": 84,
    "INA": 85,
    "IND": 86,
    "IRI": 87,
    "IRL": 88,
    "IRQ": 89,
    "ISL": 90,
    "ISR": 91,
    "ISV": 92,
    "ITA": 93,
    "IVB": 94,
    "JAM": 95,
    "JOR": 96,
    "JPN": 97,
    "KAZ": 98,
    "KEN": 99,
    "KGZ": 100,
    "KIR": 101,
    "KOR": 102,
    "KOS": 103,
    "KSA": 104,
    "KUW": 105,
    "LAO": 106,
    "LAT": 107,
    "LBA": 108,
    "LBN": 109,
    "LBR": 110,
    "LCA": 111,
    "LES": 112,
    "LIE": 113,
    "LTU": 114,
    "LUX": 115,
    "MAD": 116,
    "MAR": 117,
    "MAS": 118,
    "MAW": 119,
    "MDA": 120,
    "MDV": 121,
    "MEX": 122,
    "MGL": 123,
    "MHL": 124,
    "MKD": 125,
    "MLI": 126,
    "MLT": 127,
    "MNE": 128,
    "MON": 129,
    "MOZ": 130,
    "MRI": 131,
    "MTN": 132,
    "MYA": 133,
    "NAM": 134,
    "NCA": 135,
    "NED": 136,
    "NEP": 137,
    "NGR": 138,
    "NIG": 139,
    "NOR": 140,
    "NRU": 141,
    "NZL": 142,
    "OMA": 143,
    "PAK": 144,
    "PAN": 145,
    "PAR": 146,
    "PER": 147,
    "PHI": 148,
    "PLE": 149,
    "PLW": 150,
    "PNG": 151,
    "POL": 152,
    "POR": 153,
    "PRK": 154,
    "PUR": 155,
    "QAT": 156,
    "ROU": 157,
    "RSA": 158,
    "RUS": 159,
    "RWA": 160,
    "SAM": 161,
    "SEN": 162,
    "SEY": 163,
    "SGP": 164,
    "SKN": 165,
    "SLE": 166,
    "SLO": 167,
    "SMR": 168,
    "SOL": 169,
    "SOM": 170,
    "SRB": 171,
    "SRI": 172,
    "SSD": 173,
    "STP": 174,
    "SUD": 175,
    "SUI": 176,
    "SUR": 177,
    "SVK": 178,
    "SWE": 179,
    "SWZ": 180,
    "SYR": 181,
    "TAN": 182,
    "TGA": 183,
    "THA": 184,
    "TJK": 185,
    "TKM": 186,
    "TLS": 187,
    "TOG": 188,
    "TPE": 189,
    "TTO": 190,
    "TUN": 191,
    "TUR": 192,
    "TUV": 193,
    "UAE": 194,
    "UGA": 195,
    "UKR": 196,
    "URU": 197,
    "USA": 198,
    "UZB": 199,
    "VAN": 200,
    "VEN": 201,
    "VIE": 202,
    "VIN": 203,
    "YEM": 204,
    "ZAM": 205,
    "ZIM": 206 //津巴布韦
};
IpUtil.ipCountryMap = {};
